<template>
	<div class="tmpl">
		<!-- 头部 -->
		<div class="header">
			<span>豆瓣</span>
			<router-link class="active search" to='/search'><span class="mui-icon mui-icon-search"></span></router-link>
			<router-link class="active login" to='/login'><span class="mui-icon mui-icon-paperplane"></span></router-link>
		</div>
		<!-- 在头部增加一个返回按钮 -->
		<div v-show="isshow" class="goback">
			<a @click="goback">返回</a>
		</div>
		<!-- 路由占位 -->
		<router-view></router-view>

		<!-- 底部 -->
		<nav class="mui-bar mui-bar-tab">
			<router-link class="mui-tab-item" to="/Home">
				<span class="mui-icon mui-icon-home"></span>
				<span class="mui-tab-label">首页</span>
			</router-link>
			<router-link class="mui-tab-item" to="/login">
				<span class="mui-icon mui-icon-email">
				
				</span>
				<span class="mui-tab-label">会员</span>
			</router-link>
			<router-link class="mui-tab-item" to="/shopcar">
				<span class="mui-icon mui-icon-starhalf">
					<span ref="badge" id="badge" class="mui-badge">0</span>
				</span>
				<span class="mui-tab-label">购物车</span>
			</router-link>
			<router-link class="mui-tab-item" to="/search">
				<span class="mui-icon mui-icon-search"></span>
				<span class="mui-tab-label">搜索</span>
			</router-link>
		</nav>
	</div>
</template>

<script>
	import {vm,KEY} from './kits/sendparams.js';
	import {getTotalCount} from './kits/shopcar.js';

	vm.$on(KEY,function (input) {
		//将count设置到badge中
		document.getElementById('badge').innerText=input;
    });


	// 定义并且导出一个vm对象
	export default{		
		data(){
			return {
				isshow:false
			}
		},
		watch:{
			'$route':function(newRouter,oldRouter){
				// 判断 newRouter.path =='/Home' 就要将isshow设置为false
				// 否则将isshow设置为true
				if(newRouter.path.toLowerCase() =='/home'){
					this.isshow = false;
				}
				else{
					this.isshow = true;
				}
			}
		},
		methods:{
			// 实现上一页导航功能
			goback(){
				this.$router.go(-1);
			}
		},
		mounted(){
		    //当APP.vue加载完毕后从localStorage中获取数据
			var count = getTotalCount();
			this.$refs.badge.innerText=count;
		}
	}

</script>

<style scoped>
	.goback{
		position: absolute;
		top:10px;
		left:5px;
		z-index: 101;
	}

	.goback a{
		color:#00B51D;
		font-size: 14px;
		font-weight: bold;
	}
	.header{
		height: 50px;
		line-height: 50px;
		text-align: center;
		position: relative;
		font-family: 'microsoft yahei';
	}
	.tmpl{
		margin-top: 0px;
	}
	.login{
		position: absolute;
		right: 4%;
		color:#00B51D;
	}
	.search{
		position: absolute;
		right: 20%;
		color:#00B51D;
	}

</style>